import http from '@ohos.net.http';

export class Request {
  baseUrl: string = 'http://222.85.188.45:18000/jdyhgl/';

  createGetHttp(url: string) {
    return new Promise((resolve, reject) => {
      let httpRequest = http.createHttp()
      httpRequest.request(url, {
        method: http.RequestMethod.GET,
        expectDataType: http.HttpDataType.OBJECT })
        .then((res) => {
          if (res.responseCode === 200) {
            resolve(JSON.stringify(res.result))
          } else {
            resolve(JSON.stringify(JSON.stringify(res)))
          }
        }).catch(err => {
        reject(err.responseCode)
      })
    })
  }

  createPostHttp(url, data) {
    return new Promise((resolve, reject) => {
      let httpRequest = http.createHttp()
      httpRequest.request(this.baseUrl + url, {
        method: http.RequestMethod.POST,
        extraData: data
      }).then((res: http.HttpResponse) => {
        if (res.responseCode === 200) {
          resolve(JSON.stringify(res.result))
        } else {
          resolve(JSON.stringify(JSON.stringify(res)))
        }
      }).catch(err => {
        reject(err.responseCode)
      })
    })
  }
}


const request = new Request();

export default request as Request